class ItemRating < ActiveRecord::Base
  belongs_to :item
  has_many :replies, :class_name => 'ItemRatingReply', :foreign_key => :item_comment_id, :dependent => :destroy
  has_one :vole, :class_name => 'ItemRatingVole', :foreign_key => :item_comment_id,  :dependent => :destroy
  
  STATUSES = { 0 => '默认', 2 => '首页推荐', -1 => '删除' }
  
  named_scope :valid, :conditions => ['status >= ?', 0]
  named_scope :limit, lambda { |size| { :limit => size } }
  
  named_scope :filter, lambda { |params|
    c, h = [], {}
    
    unless params[:id].blank?
      c << "id LIKE :id"
      h[:id] = "%#{params[:id]}%"
    end
    unless params[:title].blank?
      c << "title LIKE :title"
      h[:title] = "%#{params[:title]}%"
    end
    unless params[:status].blank?
      c << "status = :status"
      h[:status] = params[:status].to_i
    end
    
    { :conditions => [c.join(' AND '), h] }
  }
  
  class << self
    
    def status_options
      STATUSES.inject([]){ |arr, obj| arr << obj.reverse }
    end
    
  end
  
  def to_s
    title
  end
  
  def display_status
    STATUSES[status]
  end
  
  def del
    update_attributes!(:status => -1)
  end
end
